clear all

use "study1.dta"

svyset [pw=wgt]

*to create fig 1

rename plusminussteal steal2
rename treatsteal steal_condition
rename rep3 pid3
recode pid3 1=1 2=3 3=2
drop if pid3==3

collapse (mean) meansteal= steal2 (sd) sdsteal=steal2 (count) n=steal2 [iw=wgt], by(steal_condition)

generate histeal = meansteal + invttail(n-1,0.025)*(sdsteal / sqrt(n))
generate lowsteal = meansteal - invttail(n-1,0.025)*(sdsteal / sqrt(n))

generate pidcond = 2 if steal_cond==1 
replace  pidcond = 5 if steal_cond==4 
replace  pidcond = 8 if steal_cond==5 
replace  pidcond = 11 if steal_cond==2
replace  pidcond = 14 if steal_cond==3 
replace  pidcond = 17 if steal_cond==6

sort pidcond
list pidcond steal_cond

format meansteal %3.2f

twoway (bar meansteal pidcond,color(gs1) mlabel(meansteal) mlabposition(2) mlabcolor(black)) ///
       (rcap histeal lowsteal pidcond), ///
       xlabel( 2 "Dems More" 5 "Dems Less" 8 "Dems Both" 11 "Reps More" 14 "Reps Less" 17 "Reps Both", noticks) ///
       xtitle("Treatment") ytitle("Proportion say tactic was attempt to steal election") ///
		ylab(0(.1)1, angle(horizontal)) ///
	   graphregion(fcolor(white)) ///
	   legend(off)
	
	    graph save figure1, replace

clear all
use "study1.dta" 

***to create fig 2

rename plusminussteal steal2
rename treatsteal steal_condition
rename rep3 pid3
recode pid3 1=1 2=3 3=2
drop if pid3==3

collapse (mean) meansteal= steal2 (sd) sdsteal=steal2 (count) n=steal2 [iw=wgt], by(pid3 steal_condition)

generate histeal = meansteal + invttail(n-1,0.025)*(sdsteal / sqrt(n))
generate lowsteal = meansteal - invttail(n-1,0.025)*(sdsteal / sqrt(n))

generate pidcond = pid3 if steal_cond==1 
replace  pidcond = pid3+4 if steal_cond==4 
replace  pidcond = pid3+8 if steal_cond==5 
replace  pidcond = pid3+12 if steal_cond==2
replace  pidcond = pid3+16 if steal_cond==3 
replace  pidcond = pid3+20 if steal_cond==6

sort pidcond
list pidcond steal_cond pid3, sepby(steal_cond)

format meansteal %3.2f
	   
twoway (bar meansteal pidcond if pid3==1,color(gs1) mlabel(meansteal) mlabposition(10) mlabcolor(black)) ///
       (bar meansteal pidcond if pid3==2,color(gs4) mlabel(meansteal) mlabposition(2) mlabcolor(black)) ///
       (rcap histeal lowsteal pidcond), ///
       legend(order(1 "Dems" 2 "Reps")) ///
       xlabel(-.05 " " 2 "Dems More" 6 "Dems Less" 10 "Dems Both" 14 "Reps More" 18 "Reps Less" 22 "Reps Both", noticks) ///
       xtitle("Treatment Type") ytitle("Proportion say tactic was attempt to steal election") ///
	   ylab(0(.1)1, angle(horizontal)) ///
	   graphregion(fcolor(white)) 
	   
	   
	   graph save figure2, replace

 
clear all
use "study1.dta"
 

* for first column of table in appendix 3

svy: logit plusminussteal c.dem##demcheat c.rep##demcheat add

* equivalent model with diff specification used to create fig 5a

svy: logit plusminussteal i.rep3##demcheat add

label define party 1 "Democrat" 2 "Independent" 3 "Republican"
label values rep3 party

margins, at(demcheat=(0 1) rep3=(1 2 3))
marginsplot, ///
 plot1opts(msymbol(O)  mcolor(gs1)  lpattern(l) lcolor(gs1)) ///
 plot2opts(msymbol(T)  mcolor(gs1)  lpattern(-) lcolor(gs1)) ///
  plot3opts(msymbol(S)  mcolor(gs1)  lpattern(_-) lcolor(gs1)) ///
   ciopts(lcolor(black)) ///
      xlabel( .05 "Reps cheat" .95 "Dems cheat", noticks) ///
      ytitle("Predicted probability was attempt to steal election") ///
	   xtitle("") ///
		ylab(0(.1)1, angle(horizontal)) ///
		title("") ///
	   graphregion(fcolor(white)) 

	   graph save figure5a, replace
	   
margins, at(add=(0 1))   

*regressions behind effects graphed in fig 7, used excel to graph differences in margins, tests of sig from parameters in models 

 svy: logit plusminussteal i.rep3##demcheat if lowhate==1
 
 margins, at(demcheat=(0 1) rep3=(1 2 3))

 
 svy: logit plusminussteal i.rep3##demcheat if highhate==1
 
 margins, at(demcheat=(0 1) rep3=(1 2 3))


***To create fig 6a (this makes a variable that is 1 if reps are cheating and 0 if dems are cheating)

rename plusminussteal steal2
rename treatsteal steal_condition
rename rep3 pid3
recode pid3 1=1 2=3 3=2

recode steal_condition 1 4 5=0 2 3 6=1
collapse (mean) meansteal= steal2 (sd) sdsteal=steal2 (count) n=steal2 [iw=wgt], by(pid3 steal_condition)

generate histeal = meansteal + invttail(n-1,0.025)*(sdsteal / sqrt(n))
generate lowsteal = meansteal - invttail(n-1,0.025)*(sdsteal / sqrt(n))

generate pidcond = 1 if steal_cond==0 & pid3 == 1
replace pidcond = 2 if steal_cond==1 & pid3 == 2
replace pidcond = 4 if steal_cond==0 & pid3 == 3
replace pidcond = 5 if steal_cond==1 & pid3 == 3
replace pidcond = 7 if steal_cond==0 & pid3 == 2
replace pidcond = 8 if steal_cond==1 & pid3 == 1

sort pidcond
list pidcond steal_cond pid3, sepby(pid3)

format meansteal %3.2f

twoway (bar meansteal pidcond if steal_cond==1,color(gs1) mlabel(meansteal) mlabposition(2) mlabcolor(black)) ///
       (bar meansteal pidcond if steal_cond==0,color(gs4) mlabel(meansteal) mlabposition(2) mlabcolor(black)) ///
       (rcap histeal lowsteal pidcond), ///
       legend( order(1 "Dems cheat" 2 "Reps cheat") ) ///
      xlabel( 1.5 "Own Party Cheats" 4.5 "Independents" 7.5 "Other Party Cheats", noticks) ///
      ytitle("Proportion say tactic was attempt to steal election") ///
	   xtitle("") ///
		ylab(0(.1)1, angle(horizontal)) ///
	   graphregion(fcolor(white)) 
	   
	   
	   graph save figure6a, replace



 
 
 
fin

